package com.u2b.business.status;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import orm.UpdateProxy;

import com.u2b.beans.Comment;
import com.u2b.beans.Status;
import com.u2b.business.location.DistanceService;
import com.u2b.business.net.FileUpload;
import com.u2b.business.time.TimeService;

import com.u2b.db.datasource.MyBeanUtils;
import com.u2b.db.status.StatusDaoImpl;
import com.u2b.global.utils.Constants;

public class StatusService
{
    private final StatusDaoImpl sdip;
    private final FileUpload fileUpload;
    private final MyBeanUtils beanUtils;

    public StatusService()
    {
        sdip = new StatusDaoImpl();
        fileUpload = new FileUpload();
        beanUtils = new MyBeanUtils();

    }

    public int addStatus_1(String[] textParams, HttpServletRequest request)
    {

        String[] urlNames = new String[]
        { "photo_URL", "audio_URL" };
        String[] mimes = new String[]
        { ".jpg", ".amr" };
        HashMap<String, Object> value = fileUpload.Upload(request, textParams, mimes, urlNames, "status");
        System.out.println("addStatus的状态的值：" + value.size());
        new TimeService().addCreateTime(value);
        int primaryKey = sdip.addStatus(value);
        return primaryKey;

    }

    public int addStatus_2(String[] textParams, HttpServletRequest request)
    {
        String[] urlNames = new String[]
        { "photo_URL", "video_URL" };
        String[] mimes = new String[]
        { ".jpg", ".3gp" };
        HashMap<String, Object> value = fileUpload.Upload(request, textParams, mimes, urlNames, "status");
        System.out.println("addStatus的状态的值：" + value.size());
        new TimeService().addCreateTime(value);
        int primaryKey = sdip.addStatus(value);
        return primaryKey;

    }

    public List<Status> getStatusList(HashMap<String, Object> initValues)
    {
        DistanceService distanceProxy = new DistanceService();
        int page = Integer.valueOf(initValues.get("page").toString());
        // 如果Page==1就更新一次
        if (page == 1)
        {
            distanceProxy.updateDistanceToKVDB(initValues); // 这个更新肯定是表示User的,也就是具体的User.注意要传一个uid
        }
        List<HashMap<String, Object>> result = sdip.getLastStatus(page);
        // 按照距离排序.50/还是按照时间排序呢?在1个geohash圈内部,距离与创建时间是有比重的.这个2.0版本先这样做吧!
       new TimeService().isExpire(result);
        List<Status> statuses = new ArrayList<Status>();
        for (int i = 0; i < result.size(); i++)
        {
            Status status = new Status();
            Map<String, Object> values = result.get(i);
            beanUtils.setPropertiesFromMap(values, status);
            statuses.add(status);
        }
        System.out.println("传回了多少条数据：" + statuses.size());
        return statuses;
    }

    public int commentThis(HashMap<String, Object> initValue)
    {

        new TimeService().addCreateTime(initValue);
        int flag = sdip.commentThis(initValue);
        if (flag > 0)
        {
            String sid = (String) initValue.get("sid");
            String sql_s = "update status_db set current_comment_num=current_comment_num+1 where sid=" + sid;
            new UpdateProxy().update(sql_s);
        }
        return flag;
     

    }

    public List<Comment> getDetailedMessage(int sid)
    {
        List<HashMap<String, Object>> result = sdip.getDetailedMessage(sid);
        List<Comment> comments = beanUtils.setPropertiesFromList(result, Comment.class.getName());
        return comments;

    }
}
